*WIC co-op eWIC new diff in diff analysis
*last modified: 13 May 2025
*last modified by: Charlotte Ambrozek
*this do file implements the csdid2 package to estimate individual 2x2 doubly robust did estimates and aggregate to obtain an att 

clear all
set more off
set rmsg on

set maxvar 120000
set emptycells drop

local data_dir ./data/cleaned
local raw_dir ./data/raw
local out_dir ./analysis/output
local graph_dir ./analysis/output/graphs
local tab_dir ./analysis/output/tables
local log_dir ./documentation/logs
local date: display %tdYY-NN-DD date(c(current_date), "DMY")
di "`date'"
capture log close

log using `log_dir'/ewic_csdid_tip`date', replace

*import cleaned eWIC implementation info into own frame
*eWIC implementation variable is at county fips/fiscal year level 
*partition data into state/treatment year groups

*import cleaned ewic implementation info into own frame
mkf ewic_fy
frame ewic_fy{
	use ./data/cleaned/ewic_rollout.dta, clear
	rename year fiscalyear
	drop pre* post* 
}

mkf income
frame income{
	use ./data/cleaned/countyyearmedianincome.dta, clear
	keep if year == 2005
	drop year
	destring ct_fips, replace
}

mkf countychars
frame countychars{
	use ./data/cleaned/countystats.dta, clear
	keep if year == 2005
	drop year
	destring ct_fips, replace
}

mkf demos
frame demos{
	use ./data/cleaned/demos.dta, clear
	keep if year == 2005
	drop year
	destring ct_fips, replace
}

cwf ewic_fy
frame put ct_fips ev_year state, into(timings)

cwf timings
duplicates drop
encode state, generate(state_code)

frlink m:1 ct_fips, frame(countychars) generate(countylink)
frget medincome sharepoor shareltwic sharelt2fpl sharesnap sharecash sharecashorsnap, from(countylink)
frlink m:1 ct_fips, frame(demos) generate(demoslink)
frget total_pop under5_pop hispanic_total_pop total_black_pop region division urbanrural share_black share_hispanic share_under5, from(demoslink)
gen log_pop = log(total_pop)
reg ev_year log_pop c.share_under5##c.shareltwic share_black share_hispanic sharesnap sharecash medincome i.region i.urbanrural
outreg2 using ./analysis/output/tables/timingregression.tex, replace
reg ev_year log_pop c.share_under5##c.shareltwic share_black share_hispanic sharesnap sharecash medincome i.urbanrural i.state_code
outreg2 using ./analysis/output/tables/timingregressionwinstate.tex, replace

*import TIP store data into own frame
mkf tip_fy
frame tip_fy{
	local data_dir ./data/cleaned
	use `data_dir'/tip_auth_sq, clear
	*drop "Direct Distribution Center" and "Home Food Delivery Contractor" types
	gen f = (vendor_type1 == 3 & (vendor_type2 == 4 | missing(vendor_type2))) | (vendor_type1 == 4 & (vendor_type2 == 3 | missing(vendor_type2)))
	bys tip_id: egen flag = mode(f)
	bys tip_id: replace vendor_type1 = vendor_type1[_n+1] if flag == 0 & f == 1
	drop if flag == 1
	drop flag f tip_year_id
	*fill in gaps in state fips using modes
	generate str_fips = string(ct_fips)
	replace str_fips = "0" + str_fips if strlen(str_fips) == 4
	generate st_fips = substr(str_fips, 1, 2)
	destring st_fips, replace 
	
	*drop Vermont and mississippi because of different regiemes prior to eWIC
	*drop NV because ewic turn on then off then on again and we don't have reliable timings
	*drop missing st_fips
	drop if st_fips == 50 | st_fips == 32 | st_fips == 28 | missing(st_fips)

	*flag stores that specialize in WIC
	gen a50 = vendor_type1 == 1 | vendor_type1 == 7 | vendor_type2 == 1 | vendor_type2 == 7
	
	*link in low income/high poverty info
	frlink m:1 ct_fips, frame(income) generate(inc_link)
	frget lowinc highpov highwicelig, from(inc_link)

	*link in controls from timings frame
	frlink m:1 ct_fips, frame(timings) generate(timings_link)
	frget total_pop under5_pop hispanic_total_pop total_black_pop region division urbanrural share_black share_hispanic share_under5 medincome sharepoor shareltwic sharelt2fpl sharesnap sharecash sharecashorsnap, from(timings_link)

	*link ewic implementation info
	frlink m:1 ct_fips fiscalyear, frame(ewic_fy) generate(ewic_link)
	frget ev_year , from(ewic_link)
	* assume treated at earliest exposure
	bys tip_id: egen ey_min = min(ev_year)
	
	* assume treated at earliest exposure
	replace ev_year = ey_min if ev_year != ey_min & !missing(ey_min)
	bys tip_id (ev_year): assert ev_year[1] == ev_year[_N]

	*assert no dups at future level of xtset 
	duplicates report tip_id fiscalyear 
	assert r(N) == r(unique_value)
	xtset tip_id fiscalyear
	compress
}

cwf tip_fy
frame drop ewic_fy

preserve
unique tip_id
local denom = r(unique_value)
/*
describe observations where we observe a balanced 4 by 4 panel in event time for event years 2009 to 2014
*/
gen miss = missing(auth) | missing(tip_id) | missing(fiscalyear) | missing(ev_year) | missing(st_fips)

bys tip_id: egen unbal = max(miss)
drop if unbal == 1

drop if !inrange(ev_year, 2009, 2014)

unique tip_id
local numerator = r(unique_value)
di `numerator'/`denom'

restore

mkf csdidsimple float(b se pval ll ul) str25(outcome subsample state controls)
mkf csdidevent float(b se pval ll ul) int(ev_yr) str25(outcome subsample state controls)
mkf csdideventavg float(avgb avgse avgpval avgll avgul) int(times) str25(time outcome subsample state controls)
mkf csdidgroup float(b se pval ll ul) str25(gr_yr outcome subsample state controls)

cwf tip_fy
cd `out_dir'
csdid auth , ivar(tip_id) time(fiscalyear) gvar(ev_year) notyet long2 saverif(csdid_tip_auth) replace cluster(st_fips)
mkf post
cwf post
use csdid_tip_auth, clear
csdid_stats simple
frame post csdidsimple (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) ("auth") ("all") ("all") ("no")
csdid_stats event, window(-4 4)
*the structure of the returned matrix here is columns pre_avg post_avg tm4 tm3 ... tm1 tp0 tp1 ... tp4
*i assume that tm# is pre period # and tp# is post period #
* so we want to capture columns 3 through 11 for the event periods, plus the pre/post averages (in a separate frame that can be linked on later).
frame post csdideventavg (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) (4) ("pre") ("auth") ("all") ("all") ("no")
frame post csdideventavg (r(table)[1,2]) (r(table)[2,2]) (r(table)[4,2]) (r(table)[5,2]) (r(table)[6,2]) (5) ("post") ("auth") ("all") ("all") ("no")
local i = 2
forval y = -4/4{
	local i = `i' + 1
	frame post csdidevent (r(table)[1,`i']) (r(table)[2,`i']) (r(table)[4,`i']) (r(table)[5,`i']) (r(table)[6,`i']) (`y') ("auth") ("all") ("all") ("no")
}
csdid_stats group
local names: colnames r(table)
local names = subinstr("`names'", "G", "", .)
di "`names'"
forval y = 1/13{
	local year: word `y' of `names'
	frame post csdidgroup (r(table)[1,`y']) (r(table)[2,`y']) (r(table)[4,`y']) (r(table)[5,`y']) (r(table)[6,`y']) ("`year'") ("auth") ("all") ("all") ("no")
}
* same estimation but with covariates for local (county level) economic conditions now
cwf tip_fy
csdid auth medincome hispanic_total_pop total_black_pop sharepoor shareltwic sharecashorsnap total_pop under5_pop, ivar(tip_id) time(fiscalyear) gvar(ev_year) notyet long2 saverif(csdid_tip_auth_controls) replace cluster(st_fips)
cwf post
use csdid_tip_auth_controls, clear
csdid_stats simple
frame post csdidsimple (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) ("auth") ("all") ("all") ("yes")
csdid_stats event, window(-4 4)
*the structure of the returned matrix here is columns pre_avg post_avg tm4 tm3 ... tm1 tp0 tp1 ... tp4
*i assume that tm# is pre period # and tp# is post period #
* so we want to capture columns 3 through 11 for the event periods, plus the pre/post averages (in a separate frame that can be linked on later).
frame post csdideventavg (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) (4) ("pre") ("auth") ("all") ("all") ("yes")
frame post csdideventavg (r(table)[1,2]) (r(table)[2,2]) (r(table)[4,2]) (r(table)[5,2]) (r(table)[6,2]) (5) ("post") ("auth") ("all") ("all") ("yes")
local i = 2
forval y = -4/4{
	local i = `i' + 1
	frame post csdidevent (r(table)[1,`i']) (r(table)[2,`i']) (r(table)[4,`i']) (r(table)[5,`i']) (r(table)[6,`i']) (`y') ("auth") ("all") ("all") ("yes")
}

csdid_stats group
local names: colnames r(table)
local names = subinstr("`names'", "G", "", .)
forval y = 1/13{
	local year: word `y' of `names'
	frame post csdidgroup (r(table)[1,`y']) (r(table)[2,`y']) (r(table)[4,`y']) (r(table)[5,`y']) (r(table)[6,`y']) ("`year'") ("auth") ("all") ("all") ("yes")
}

*estimation for processor graphs
*need state specific estimates to link back in processor order
*make a temp frame that is state of interest plus all states treated only after data end;
* then estimates represent treatment effect in state
* don't save event study estimates here, because we'll just need the state average treatment effect for the plot
foreach state in "VA" "MI" "OK" "IN" "OH" "SD" "CT" "NY" "NM" "TX" "MD" "MT" "NC" "AR" "MA" "WV" "FL" "KY" "WI" "OR" "DE" "CO" "IA" "KS" "AZ" "NE" {
	cwf tip_fy
	frame put _all if ((state == "`state'") | inlist(st_fips, 1, 2, 6, 11, 13, 15, 16, 17, 22, 23, 27, 29, 33, 34, 38, 42, 44, 45, 47, 49, 53)), into(state)
	cwf state
	csdid auth, ivar(tip_id) time(fiscalyear) gvar(ev_year) notyet long2 saverif(csdid_tip_auth_`state') replace cluster(st_fips)
	cwf post
	use csdid_tip_auth_`state', clear
	if "`state'" == "NH" list in 1/20
	csdid_stats simple
	frame post csdidsimple (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) ("auth") ("all") ("`state'") ("no")
	cwf state
	csdid auth medincome hispanic_total_pop total_black_pop sharepoor shareltwic sharecashorsnap total_pop under5_pop, ivar(tip_id) time(fiscalyear) gvar(ev_year) notyet long2 saverif(csdid_tip_auth_controls_`state') replace cluster(st_fips)
	cwf post
	use csdid_tip_auth_controls_`state', clear 
	if "`state'" == "NH" list in 1/20
	csdid_stats simple
	frame post csdidsimple (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) ("auth") ("all") ("`state'") ("yes")
	frame drop state
}

cwf tip_fy
frame put _all if chain == 1, into(chain)
cwf chain

cd `out_dir'
csdid auth, ivar(tip_id) time(fiscalyear) gvar(ev_year) notyet long2 saverif(csdid_tip_authchain) replace cluster(st_fips)
cwf post
use csdid_tip_authchain, clear
csdid_stats simple
frame post csdidsimple (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) ("auth") ("chain") ("all") ("no")
csdid_stats event, window(-4 4)
*the structure of the returned matrix here is columns pre_avg post_avg tm4 tm3 ... tm1 tp0 tp1 ... tp4
*i assume that tm# is pre period # and tp# is post period #
* so we want to capture columns 3 through 11 for the event periods, plus the pre/post averages (in a separate frame that can be linked on later).
frame post csdideventavg (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) (4) ("pre") ("auth") ("chain") ("all") ("no")
frame post csdideventavg (r(table)[1,2]) (r(table)[2,2]) (r(table)[4,2]) (r(table)[5,2]) (r(table)[6,2]) (5) ("post") ("auth") ("chain") ("all") ("no")
local i = 2
forval y = -4/4{
	local i = `i' + 1
	frame post csdidevent (r(table)[1,`i']) (r(table)[2,`i']) (r(table)[4,`i']) (r(table)[5,`i']) (r(table)[6,`i']) (`y') ("auth") ("chain") ("all") ("no")
}
csdid_stats group
local names: colnames r(table)
local names = subinstr("`names'", "G", "", .)
forval y = 1/13{
	local year: word `y' of `names'
	frame post csdidgroup (r(table)[1,`y']) (r(table)[2,`y']) (r(table)[4,`y']) (r(table)[5,`y']) (r(table)[6,`y']) ("`year'") ("auth") ("chain") ("all") ("no")
}
* chain only analysis but with controls this time
cwf chain
csdid auth medincome hispanic_total_pop total_black_pop sharepoor shareltwic sharecashorsnap total_pop under5_pop, ivar(tip_id) time(fiscalyear) gvar(ev_year) notyet long2 saverif(csdid_tip_authchain_controls) replace cluster(st_fips)
cwf post
use csdid_tip_authchain_controls, clear
csdid_stats simple
frame post csdidsimple (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) ("auth") ("chain") ("all") ("yes")
csdid_stats event, window(-4 4)
*the structure of the returned matrix here is columns pre_avg post_avg tm4 tm3 ... tm1 tp0 tp1 ... tp4
*i assume that tm# is pre period # and tp# is post period #
* so we want to capture columns 3 through 11 for the event periods, plus the pre/post averages (in a separate frame that can be linked on later).
frame post csdideventavg (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) (4) ("pre") ("auth") ("chain") ("all") ("yes")
frame post csdideventavg (r(table)[1,2]) (r(table)[2,2]) (r(table)[4,2]) (r(table)[5,2]) (r(table)[6,2]) (5) ("post") ("auth") ("chain") ("all") ("yes")
local i = 2
forval y = -4/4{
	local i = `i' + 1
	frame post csdidevent (r(table)[1,`i']) (r(table)[2,`i']) (r(table)[4,`i']) (r(table)[5,`i']) (r(table)[6,`i']) (`y') ("auth") ("chain") ("all") ("yes")
}
csdid_stats group
local names: colnames r(table)
local names = subinstr("`names'", "G", "", .)
forval y = 1/13{
	local year: word `y' of `names'
	frame post csdidgroup (r(table)[1,`y']) (r(table)[2,`y']) (r(table)[4,`y']) (r(table)[5,`y']) (r(table)[6,`y']) ("`year'") ("auth") ("chain") ("all") ("yes")
}

*estimation for processor graphs
*need state specific estimates to link back in processor order
*make a temp frame that is state of interest plus all states treated only after data end;
* then estimates represent treatment effect in state
* don't save event study estimates here, because we'll just need the state average treatment effect for the plot
foreach state in "VA" "MI" "OK" "IN" "OH" "SD" "CT" "NY" "NM" "TX" "MD" "MT" "NC" "AR" "MA" "WV" "FL" "KY" "WI" "OR" "DE" "CO" "IA" "KS" "AZ" "NE" {
	cwf chain
	frame put _all if ((state == "`state'") | inlist(st_fips, 1, 2, 6, 11, 13, 15, 16, 17, 22, 23, 27, 29, 33, 34, 38, 42, 44, 45, 47, 49, 53)), into(state)
	cwf state
	csdid auth, ivar(tip_id) time(fiscalyear) gvar(ev_year) notyet long2 saverif(csdid_tip_authchain_`state') replace cluster(st_fips)
	cwf post 
	use csdid_tip_authchain_`state', clear
	csdid_stats simple
	frame post csdidsimple (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) ("auth") ("chain") ("`state'") ("no")
	cwf state
	csdid auth medincome hispanic_total_pop total_black_pop sharepoor shareltwic sharecashorsnap total_pop under5_pop, ivar(tip_id) time(fiscalyear) gvar(ev_year) notyet long2 saverif(csdid_tip_authchain_controls_`state') replace cluster(st_fips)
	cwf post 
	use csdid_tip_authchain_controls_`state', clear
	csdid_stats simple
	frame post csdidsimple (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) ("auth") ("chain") ("`state'") ("yes")
	cwf tip_fy
	frame drop state
}

cwf tip_fy
frame put _all if chain == 0, into(indep)
cwf indep
frame drop chain
* indepedent store estimation
cd `out_dir'
csdid auth, ivar(tip_id) time(fiscalyear) gvar(ev_year) notyet long2 saverif(csdid_tip_authindep) replace cluster(st_fips)
cwf post 
use csdid_tip_authindep, clear
csdid_stats simple
frame post csdidsimple (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) ("auth") ("indep") ("all") ("no")
csdid_stats event, window(-4 4)
*the structure of the returned matrix here is columns pre_avg post_avg tm4 tm3 ... tm1 tp0 tp1 ... tp4
*i assume that tm# is pre period # and tp# is post period #
* so we want to capture columns 3 through 11 for the event periods, plus the pre/post averages (in a separate frame that can be linked on later).
frame post csdideventavg (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) (4) ("pre") ("auth") ("indep") ("all") ("no")
frame post csdideventavg (r(table)[1,2]) (r(table)[2,2]) (r(table)[4,2]) (r(table)[5,2]) (r(table)[6,2]) (5) ("post") ("auth") ("indep") ("all") ("no")
local i = 2
forval y = -4/4{
	local i = `i' + 1
	frame post csdidevent (r(table)[1,`i']) (r(table)[2,`i']) (r(table)[4,`i']) (r(table)[5,`i']) (r(table)[6,`i']) (`y') ("auth") ("indep") ("all") ("no")
}
csdid_stats group
local names: colnames r(table)
local names = subinstr("`names'", "G", "", .)
forval y = 1/13{
	local year: word `y' of `names'
	frame post csdidgroup (r(table)[1,`y']) (r(table)[2,`y']) (r(table)[4,`y']) (r(table)[5,`y']) (r(table)[6,`y']) ("`year'") ("auth") ("indep") ("all") ("no")
}
*independent store estimation with covariates
cwf indep
csdid auth medincome hispanic_total_pop total_black_pop sharepoor shareltwic sharecashorsnap total_pop under5_pop, ivar(tip_id) time(fiscalyear) gvar(ev_year) notyet long2 saverif(csdid_tip_authindep_controls) replace cluster(st_fips)
cwf post
use csdid_tip_authindep_controls, clear
csdid_stats simple
frame post csdidsimple (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) ("auth") ("indep") ("all") ("yes")
csdid_stats event, window(-4 4)
*the structure of the returned matrix here is columns pre_avg post_avg tm4 tm3 ... tm1 tp0 tp1 ... tp4
*i assume that tm# is pre period # and tp# is post period #
* so we want to capture columns 3 through 11 for the event periods, plus the pre/post averages (in a separate frame that can be linked on later).
frame post csdideventavg (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) (4) ("pre") ("auth") ("indep") ("all") ("yes")
frame post csdideventavg (r(table)[1,2]) (r(table)[2,2]) (r(table)[4,2]) (r(table)[5,2]) (r(table)[6,2]) (5) ("post") ("auth") ("indep") ("all") ("yes")
local i = 2
forval y = -4/4{
	local i = `i' + 1
	frame post csdidevent (r(table)[1,`i']) (r(table)[2,`i']) (r(table)[4,`i']) (r(table)[5,`i']) (r(table)[6,`i']) (`y') ("auth") ("indep") ("all") ("yes")
}
csdid_stats group
local names: colnames r(table)
local names = subinstr("`names'", "G", "", .)
forval y = 1/13{
	local year: word `y' of `names'
	frame post csdidgroup (r(table)[1,`y']) (r(table)[2,`y']) (r(table)[4,`y']) (r(table)[5,`y']) (r(table)[6,`y']) ("`year'") ("auth") ("indep") ("all") ("yes")
}

*estimation for processor graphs
*need state specific estimates to link back in processor order
*make a temp frame that is state of interest plus all states treated only after data end;
* then estimates represent treatment effect in state
* don't save event study estimates here, because we'll just need the state average treatment effect for the plot
foreach state in "VA" "MI" "OK" "IN" "OH" "SD" "CT" "NY" "NM" "TX" "MD" "MT" "NC" "AR" "MA" "WV" "FL" "KY" "WI" "OR" "DE" "CO" "IA" "KS" "AZ" "NE" {
	cwf indep
	frame put _all if ((state == "`state'") | inlist(st_fips, 1, 2, 6, 11, 13, 15, 16, 17, 22, 23, 27, 29, 33, 34, 38, 42, 44, 45, 47, 49, 53)), into(state)
	cwf state
	csdid auth, ivar(tip_id) time(fiscalyear) gvar(ev_year) notyet long2 saverif(csdid_tip_authindep_`state') replace cluster(st_fips)
	cwf post 
	use csdid_tip_authindep_`state', clear
	csdid_stats simple
	frame post csdidsimple (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) ("auth") ("indep") ("`state'") ("no")
	cwf state
	csdid auth medincome hispanic_total_pop total_black_pop sharepoor shareltwic sharecashorsnap total_pop under5_pop, ivar(tip_id) time(fiscalyear) gvar(ev_year) notyet long2 saverif(csdid_tip_authindep_controls_`state') replace cluster(st_fips)
	cwf post
	use csdid_tip_authindep_controls_`state', clear
	csdid_stats simple
	frame post csdidsimple (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) ("auth") ("indep") ("`state'") ("yes")
	cwf tip_fy
	frame drop state
}

/* cwf tip_fy
frame put _all if highwicelig == 1, into(highwicelig)
cwf highwicelig

cd `out_dir'
csdid auth, ivar(tip_id) time(fiscalyear) gvar(ev_year) notyet long2 saverif(csdid_tip_authhighwicelig) replace cluster(st_fips)
csdid_stats simple
frame post csdidsimple (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) ("auth") ("highwicelig") ("all") ("no")
csdid_stats event, window(-4 4)
*the structure of the returned matrix here is columns pre_avg post_avg tm4 tm3 ... tm1 tp0 tp1 ... tp4
*i assume that tm# is pre period # and tp# is post period #
* so we want to capture columns 3 through 11 for the event periods, plus the pre/post averages (in a separate frame that can be linked on later).
frame post csdideventavg (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) (4) ("pre") ("auth") ("highwicelig") ("all") ("no")
frame post csdideventavg (r(table)[1,2]) (r(table)[2,2]) (r(table)[4,2]) (r(table)[5,2]) (r(table)[6,2]) (5) ("post") ("auth") ("highwicelig") ("all") ("no")
local i = 2
forval y = -4/4{
	local i = `i' + 1
	frame post csdidevent (r(table)[1,`i']) (r(table)[2,`i']) (r(table)[4,`i']) (r(table)[5,`i']) (r(table)[6,`i']) (`y') ("auth") ("highwicelig") ("all") ("no")
}
csdid_stats group
local names: colnames r(table)
local names = subinstr("`names'", "G", "", .)
forval y = 1/10{
	local year: word `y' of `names'
	frame post csdidgroup (r(table)[1,`y']) (r(table)[2,`y']) (r(table)[4,`y']) (r(table)[5,`y']) (r(table)[6,`y']) ("`year'") ("auth") ("highwicelig") ("all") ("no")
} */

frame csdideventavg{
	expand times, generate(new)
	bys time outcome subsample controls (new): gen ev_yr = sum(new)
	replace ev_yr = ev_yr - 4 if time == "pre"
	drop time new times
}

cwf csdidevent
frlink 1:1 ev_yr outcome subsample controls, frame(csdideventavg)
frget avgb avgse avgpval avgll avgul, from(csdideventavg)
drop csdideventavg

frame drop csdideventavg

cwf csdidsimple
/* frame drop highwiceligindep */
save `out_dir'/csdid_tip_simple.dta, replace
cwf csdidevent
save `out_dir'/csdid_tip_event.dta, replace  
cwf csdidgroup
save `out_dir'/csdid_tip_group.dta, replace  
log close 
